'IACUC Amendment Requirement Script  
Option Strict Off
imports System
imports System.Collections
imports Humans = PHSRAG.Insight21.Humans
imports BusinessObjects = PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
	sub Main()

        Dim hazardExists as Boolean
        Dim tumorExists as Boolean
        Dim procExists as Boolean
        Dim anesExists as Boolean
        Dim sponsorExists as Boolean
        for each qa as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
            select case qa.QuestionnaireCategory
                case "QUESTCAT_ANM_TP"
                    tumorExists = true
                case "QUESTCAT_ANM_PRO"
                    procExists = true
                case "QUESTCAT_ANM_HZ"
                    hazardExists = true
                case "QUESTCAT_ANM_AN"
                    anesExists = true
                case "QUESTCAT_ANM_SF"
                    sponsorExists = true
            end select
        next

		Dim answers as ArrayList = currentProtVersion.QuestionnaireAnswers
		if answers.Count > 0 Then
			Dim latestIRVersion as Humans.ProtocolVersion = Humans.Protocol.GetLatestApplicationVersion (theProtocol.Id, connectionString, false, cachePolicy)
			'To avail the user a feature to add new questionnaire of a particulr form type
			Dim isMultiple as boolean = true
			Dim oldAnswers as new ArrayList()
			if not latestIRVersion is nothing then
                oldAnswers = latestIRVersion.QuestionnaireAnswers
            end if   
            'Staff has changed.
            'By default Staff tab will not be made visible. only when the 'Topchoice' question is answered 'yes', the staff tab will be made visible.
            dim amendmentInitialQA as ArrayList = GetQuestionnaireAnswerByCategory("QUESTCAT_ANM_AM")
            if amendmentInitialQA.Count > 0 then
                Dim comStaffChange as ArrayList = amendmentInitialQA(0).GetAnswers("TopChoice")
                if not comStaffChange is nothing then
                    if comStaffChange.Count > 0 then
                        currentProcess.ShowStaff = false
                        currentProcess.ShowAttachments = true
                        if comStaffChange(0).Value = "Y" then 
                            currentProcess.ShowStaff = true
                            currentProtVersion.DeleteQuestionnaireAnswer("Standard Application")
                            currentProtVersion.DeleteQuestionnaireAnswer("Abstract")
                            currentProtVersion.DeleteQuestionnaireAnswer("Detailed Research Plan")
                            currentProtVersion.DeleteQuestionnaireAnswer("Sponsor Funding")
                            currentProtVersion.DeleteQuestionnaireAnswer("Duration, Clinical Signs, Endpoints and Euthanasia")
                        else if comStaffChange(0).Value = "N" then 
                            currentProcess.ShowStaff = false
                            Dim CurrentProcessDocuments as ArrayList = currentProcess.ProtocolDocuments
                            Dim isFlowChartAvailable as Boolean = false	                                
                            for each protocolDocument as Humans.protocolDocument  in CurrentProcessDocuments 
                                if protocolDocument.Type = "ATCHTYPE_FLOWCHART" then
                                    isFlowChartAvailable = true
									exit for
       	                        end if
                            next
                            if not isFlowChartAvailable then
	                            ' Amendments must have a flowchart attachement
		                        currentProcess.AddProtocolDocument ( currentProcess.ProtocolId, "Flowchart", "ATCHTYPE_FLOWCHART","Flowchart", false, currentProcess.Id )
	                        end if                                                
                        end if
                    end if
                end if
                Dim isCreatedThrougheIACUC as ArrayList = amendmentInitialQA(0).GetAnswers("InsightProd")
                if not isCreatedThrougheIACUC is nothing then
                    if isCreatedThrougheIACUC.Count > 0 then
                        'currentProcess.ShowStaff = false
                        if (isCreatedThrougheIACUC(0).Value = "N") then 
							' Standard always gets Abstract and Detailed Research Plan
							Dim abstractQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Abstract", FormsManagement.Constants.QuestionnaireType.Animals )
							Dim abstract as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(abstractQV.Id, cachePolicy, "Abstract", true)

							Dim detailedPlanQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Detailed Research Plan", FormsManagement.Constants.QuestionnaireType.Animals )
							Dim detailedPlan as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(detailedPlanQV.Id, cachePolicy, "Detailed Research Plan", true)

							' Standard Application
							Dim standardQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Standard Application", FormsManagement.Constants.QuestionnaireType.Animals )
							Dim standard as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(standardQV.Id, cachePolicy, "Standard Application", true)

							' Sponsor Application
							if not sponsorExists then
							    Dim sponsorQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Sponsor Funding", FormsManagement.Constants.QuestionnaireType.Animals )
							    Dim sponsor as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(sponsorQV.Id, cachePolicy, "Sponsor Funding", true)
                            end if

							' Duration & clinical signs
							Dim durationClinicalSignsQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Duration, Clinical Signs, Endpoints and Euthanasia", FormsManagement.Constants.QuestionnaireType.Animals )
							Dim durationClinical as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(durationClinicalSignsQV.Id, cachePolicy, "Duration, Clinical Signs, Endpoints and Euthanasia", true)

                            ' Housing
                            Dim housingQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Housing", FormsManagement.Constants.QuestionnaireType.Animals )
                            Dim housing as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(housingQV.Id, cachePolicy, "Housing", true)

                            ' Potential Pain and Distress
                            Dim ppdQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Potential Pain and Distress", FormsManagement.Constants.QuestionnaireType.Animals )
                            Dim ppd as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(ppdQV.Id, cachePolicy, "Potential Pain and Distress", true)

                        else if (isCreatedThrougheIACUC(0).Value = "Y") then

                            ' Don't think these ifs are needed, see more succinct code below
                            Dim IsSponsorAmendment as ArrayList = amendmentInitialQA(0).GetAnswers("ComSponsor")
                            if not IsSponsorAmendment is nothing then
                                if IsSponsorAmendment.Count > 0 then
                                    'currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, "Sponsor Funding", "Sponsor Funding", oldAnswers, true, theProtocol.Id)                   
                                    'Add Sponsor form
                                    isMultiple = true
		                            AddForm("ComSponsor", "Sponsor Funding", "Sponsor Funding", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Sponsor", sponsorExists)                           
                                end if
                            end if
				            Dim IsAbstractChange as ArrayList = amendmentInitialQA(0).GetAnswers("ComAbstract")
                            if not IsAbstractChange is nothing then
                                if IsAbstractChange.Count > 0 then
                                    isMultiple = false
                                    AddForm("ComAbstract", "ABSTRACT", "Abstract", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Abstract", false)                           
                                end if
                            end if
				            Dim IsDetailsChange as ArrayList = amendmentInitialQA(0).GetAnswers("ComDetailed")
                            if not IsAbstractChange is nothing then
                                if IsAbstractChange.Count > 0 then
                                    isMultiple = false
	                                AddForm("ComDetailed", "DETAILS", "Detailed Research Plan","QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Detailed Research Plan", false)                           
                                end if
							end if
							
							AddForm("Additional", "Y", "Potential Pain and Distress","QUESTCAT_ANM_AM",oldAnswers,false,"Potential Pain and Distress", false)
							AddForm("AnimHouse", "Y", "Housing","QUESTCAT_ANM_AM",oldAnswers,false,"Housing", false)

							'Add hazards form and change the isMultiple flag to false, if it's not a multiple answerable questionnaire
							isMultiple = true
							AddForm("ComHazards", "HAZARDS", "Hazardous Agent Administration and Use","QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Hazards", hazardExists)
							'Add Procedures form
							isMultiple = false
							AddForm("ComProcedure", "PROCEDURE", "Procedures", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Procedures", procExists)
							'Add Tumor form
							'isMultiple = true
							AddForm("ComTumor", "TUMOR", "Tumor Production", "QUESTCAT_ANM_AM",oldAnswers,true,"Tumor", tumorExists) 
							'Add Ascites form
							'isMultiple = true
							AddForm("ComAscites", "ASCITES", "Antibody & Ascites Production", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Ascites", false)                             
							'Add Breeding form
							'isMultiple = true
							AddForm("ComBreeding", "BREEDING", "Breeding Studies", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Breeding", false)  
							'Add Fluid form
							'isMultiple = true
							AddForm("ComFluid", "FLUID", "Controlled Food or Fluid Intake", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Fluid", false)  
							'Add Euthanasia form
							'isMultiple = true
							AddForm("ComEuthan", "EUTHANASIA", "Duration, Clinical Signs, Endpoints and Euthanasia", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Duration, Clinical Signs, Endpoints and Euthanasia", false)                           
							'Add Euthanasia form
							'isMultiple = true
							AddForm("ComDuration", "Duration", "Duration, Clinical Signs, Endpoints and Euthanasia", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Duration, Clinical Signs, Endpoints and Euthanasia", false)                           
							'Add Anesthesia form
							isMultiple = true
							AddForm("ComAnesthesia", "ANESTHESIA", "Anesthesia Regimen", "QUESTCAT_ANM_AM",oldAnswers,isMultiple,"Anesthesia", anesExists)                           

                        end if
                    else 
                        Dim IsSponsorAmendment as ArrayList = amendmentInitialQA(0).GetAnswers("ComSponsor")
                        if not IsSponsorAmendment is nothing then
                            if IsSponsorAmendment.Count > 0 and not sponsorExists then
                                currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, "Sponsor Funding", "Sponsor Funding", oldAnswers, true, theProtocol.Id)                   
                            end if
                        end if
                    end if                            
                end if                         
            end if	                                     
                    
            theProtocol.Save( connectionString )
            currentProtVersion.Save ( connectionString )
            currentProcess.Save( connectionString )
        end if                  
	end sub  

	' Add or remove questionnaire answers to protocol version based on answer to a question
	sub AddForm(byval answerName as string, byval answerValue as string, byval questionnaireName as string, byval questionnaireCategory as string,byval oldAnswers as ArrayList,byval isMultiple as boolean, byval questionnaireAnswerName as string, byval formExists as boolean)
		dim qAs as ArrayList = GetQuestionnaireAnswerByCategory(questionnaireCategory)
		if qAs.count > 0 then
			for each qa as FormsManagement.QuestionnaireAnswer in qAs
				Dim answers as Arraylist = qA.GetAnswers(answerName)
				if answers.Count > 0 then
					if answers(0).Value = answerValue and not formExists then
                		currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
					else
						currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
					end if  					
				else
					currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
				end if
			next
		end if
	end sub


	'function to return the qAs based on the category passed in
	function GetQuestionnaireAnswerByCategory(byval category as string) 
		dim qAs as new ArrayList()
		for each qa as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
			if category =  qa.QuestionnaireCategory then
				dim str = qa.Name
				qAs.add(qa)
				'There might be more than one questionnaire answer that might be returned, so do not exit
			end if   
		next
		return qAs
	end function
	
end module
